<template>
	<view>
        <uni-apptitle contentData="登录密码设置"></uni-apptitle>
        <view class="paddinglr-30 margint-88 background-fff" style="overflow:hidden">
            <view class="size-16 margintb-40 text-center">为了您的账户安全，请设置登录密码</view>
            <uni-applist title="登录账号">
               <input slot="content" class="text-left width-m size-14 marginl-30" disabled type="number" v-model="loginNum" />
            </uni-applist>
            <uni-applist title="登录密码">
               <input slot="content" class="text-left width-m size-14 marginl-30" placeholder="6-12位数字加字母" :maxlength="12" type="password" v-model="info.loginPassowrd" />
            </uni-applist>
            <uni-applist title="确认密码">
               <input slot="content" class="text-left width-m size-14 marginl-30" placeholder="请再次输入密码" :maxlength="12" type="password" v-model="info.loginRepassowrd" />
            </uni-applist>
            <view class="base-size margintb-20 paddinglr-30 base-color-a">此密码可在后续App登录及提款时使用</view>
            <button class="confirm marginlr-30 margintb-60" @click="confirm" type="primary">确定</button>
        </view>
	</view>
</template>

<script>
    import unicore from '@/core/core.service.ts'
    import graceChecker from "@/common/graceChecker.js"
	export default {
		data() {
			return {
                info:{
                    loginPassowrd:'',
                    loginRepassowrd:'',
                }
			}
        },
        computed:{
            loginNum(){
                return this.$store.state.common.dlzh
            },
			isValid(){
				let rule = [
                    {name: 'loginPassowrd', checkType: 'boolean',checkRule:this.$core.regexp.isPassword(this.info.loginPassowrd),errorMsg: '登录密码格式不正确'},
                    {name: 'loginRepassowrd', checkType: 'boolean',checkRule:this.info.loginPassowrd==this.info.loginRepassowrd,errorMsg: '两次密码输入不一致'},
					{name: 'loginRepassowrd', checkType: 'boolean',checkRule:this.$core.regexp.isPassword(this.info.loginRepassowrd),errorMsg: '确认密码格式不正确'},
				]
				let checkRes = graceChecker.check(this.info, rule)
				return checkRes;
            },
            iswxh5(){
				return unicore.env.iswxh5
			}
        },
        created(){
            this.$store.state.common.dlzh = unicore.util.localStorage.get("dlzh")
            history.pushState(null,null,document.URL)
            window.addEventListener("popstate",(e)=>{
				history.pushState(null,null,document.URL)
			})
        },
		methods: {
			confirm(){
                if (this.isValid) {
                    let params = {
                        password:this.info.loginPassowrd,
                        token:this.$store.state.common.token,
                        wxOpenId : unicore.dom.getQueryString("wxOpenId") || unicore.util.localStorage.get("wxOpenId")
                    }
                    console.log(params,'设置密码参数')
                    this.$store.dispatch('setPasswd', params).then(res => {
                        let redirect = decodeURIComponent(unicore.dom.getQueryString("redirect"))
                        if (res.reqResult.code == 1 || res.reqResult.code == -200305){
                            unicore.util.localStorage.set("noPassword",false)
                            if (this.iswxh5&&redirect&&res.reqResult.code != -200305) {
                                // 微信h5直接跳转页面
                                return uni.redirectTo({url:redirect})
                            }else if (this.iswxh5&&res.reqResult.code == -200305){
                                uni.showToast({
                                    title:'密码设置失败，请以空手到App设置的密码为准！',
                                    icon:'none',
                                    duration:5000
                                })
                                setTimeout(() => {
                                    uni.redirectTo({url:redirect||this.$store.state.common.saveUrl})
                                }, 5000)
                                return
                            }
                            if (!this.$store.state.common.realname) {
                                return uni.redirectTo({url:'/pages/uploadId/uploadId'})
                            }
                            console.log(this.$store.state.common.saveUrl,'跳转url222')
                            uni.redirectTo({url:this.$store.state.common.saveUrl})
                        }
                    })
                } else {
					uni.showToast({
                        icon: "none",
                        title:graceChecker.error
                    })
				}
			}
		},
	}
</script>
